
// render JSON topics
function renderJSONTopics(data) {
	$.each(data, function(k, topic) { 
		var topicDiv = "";
		topicDiv = topicDiv + "  <div class='topic'>";
		topicDiv = topicDiv + "  <div class='name'><a href='dds'>" + topic['name'] + "</a></div>";
		topicDiv = topicDiv + "  <div class='description'>" + topic['description'] + "</div>";
		topicDiv = topicDiv + "  <div class='info'>";
		topicDiv = topicDiv + "    created by <a href='" + topic['user_username'] + "'>" + topic['user_username'] + "</a> on " + topic['creationdate'];
		topicDiv = topicDiv + "    &nbsp;&nbsp;-&nbsp;&nbsp;"; 
		// topicDiv = "<a href='" + topic[friendlyurl]{$topic->friendlyurl}#followers"><b>{$topic->followerscount}</b> {#topic_followers#}</a>
		topicDiv = topicDiv + "  </div>"			
		topicDiv = topicDiv + "</div>";
		$("#topiclist").append(topicDiv);
	});
} 


// render JSON message
function renderJSONMessage(dataJSON) {
	var msgDiv = "";
	msgDiv = msgDiv + "<div id=\"msg:XXXXX\" class=\"topicmessage\">";
	msgDiv = msgDiv + "<div class=\"profilepic\"><img src=\"imgs/u/1.jpg\"/></div>";
	msgDiv = msgDiv + "<div class=\"msg\">";
	msgDiv = msgDiv + " <span class=\"username\"><a href=\"user?u=\"" + dataJSON.user_username + "\">" + dataJSON.user_username + "</a></span>";
	msgDiv = msgDiv + "<span class=\"msg\">" + dataJSON.msg + "</span><br/>";
	msgDiv = msgDiv + "<div class=\"info\"><a href=\"message?id={$messageVO->id}\">{$messageVO->creationdate|date_format:\"%D %I:%M %p\"}</a> {#message_from#} web</div>";
	msgDiv = msgDiv + "</div>";
	msgDiv = msgDiv + "<div class=\"clear\"></div>";
	msgDiv = msgDiv + "</div>";	
	// $('#newmsgs').append(msgDiv);
	$('#newmsgs').prepend(msgDiv);
}


// ajax call to get topics by creation date
function showTopicsByCreationDate() {
	$.ajax({
	    data: "type=creationdate",
	    type: "GET",
	    dataType: "json",
	    url: "api/topics.php",
	    success: function(data){ 
	    	renderJSONTopics(data);
	    }
	});	
}


// when focus on search box, change the default text
function changeOnFocusSearchBox() {
	if ($('#q').val() == $('#searchbox_q_label').html()) {
		$('#q').removeClass("focus").addClass("disabled");
	}	
	$('#q').focus(function() {
		if (this.value == $('#searchbox_q_label').html()) {
			$(this).removeClass("disabled").addClass("focus");
        	this.value = '';
    	}
    });
    $('#q').blur(function() {
    	if (this.value == '') {
    		$(this).removeClass("focus").addClass("disabled");
    		this.value = $('#searchbox_q_label').html();
    	}
    });
}


// when focus on write comment on a topic, change the default text
function changeOnFocusWriteComment() {
	if ($('#msg').val() == $('#topic_write_comment_label').html()) {
		$('#msg').removeClass("focus").addClass("disabled");
	}	
	$('#msg').focus(function() {
		if (this.value == $('#topic_write_comment_label').html()) {
			$(this).removeClass("disabled").addClass("focus");
        	this.value = '';
    	}
    });
    $('#msg').blur(function() {
    	if (this.value == '') {
    		$(this).removeClass("focus").addClass("disabled");
    		this.value = $('#topic_write_comment_label').html();
    	}
    });
}


// increment followers count + 1
function followerscountPlusOne() {
	$('#followerscount').html(parseInt($('#followerscount').html()) + 1);
}


// show loading image
function showLoading() {
	$('#loading').show();
}


// hide loading image
function hideLoading() {
	$('#loading').hide();
}





// add logic to Write comment textarea
function addWriteCommentLogic() {

	$('#msg').bind('keypress', function(e) {
		if (e.keyCode == 13){
			
			showLoading();
			
			$.ajax({
			    data: "action=post&msg=" + $('#msg').val(),
			    type: "POST",
			    url: "api/topic.php",
			    success: function(data){
			    	alert(data);
			    	var dataJSON = jQuery.parseJSON(data);
			    	renderJSONMessage(dataJSON);
			    }
			});
			/*var data = new Array();
			data['user_username'] = "nico";
			data['msg'] = $('#msg').val();
			renderJSONMessage(data);*/
			
			
			//$("#msg").attr("disabled", false);

			
			// clean msg text
			$('#msg').val("");
			// $("#msg").attr("disabled", "");
			
			hideLoading();
			return false;
		}
	});
}


$(document).ready(function(){

	// showTopicsByCreationDate();
	changeOnFocusSearchBox();
	changeOnFocusWriteComment();
	
	addWriteCommentLogic();
});